<?php

namespace app\admin\validate;

use app\common\model\ProductStock;
use think\Validate;

class Purchase extends Validate
{
    protected $scene = [
        'Purchase' => ['product_id','logistics_type','stock'],
    ];

    protected $rule =   [
        'product_id' => 'require|number',
        'logistics_type' => 'require|number',
        'stock' => 'array|checkStock',
    ];

    protected $message  =   [
        'product_id.require' => '商品id必须',
        'product_id.number' => '商品id必须是数字',
        'logistics_type.require' => '物流类型必须',
        'logistics_type.number' => '物流类型有误',
        'stock.array' => '申购参数格式错误',
    ];

    protected function checkStock($value,$rule,$data=[])
    {
        if($value != 0){
            $stocks = array_filter($value,function($v){
                return $v['number'];
            });
            if(empty($stocks)){
                return '至少需要申购一种规格';
            }
        }
        return true;
    }
}
